﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;

namespace INSTA.BankApplication.DAL.Tests
{
    /// <summary>
    /// Utility class used to change configuration file keys
    /// at run-time.
    /// </summary>
    public static class ConfigFileManager
    {
        public const string KEY_DBTYPE_SQLSERVER    = "SqlServer";
        public const string KEY_DBTYPE_MYSQL        = "MySql";
        public const string KEY_DBTYPE_UNKNOWN      = "xxxxxx";

        public const string KEY_CONNECTION_STRING = "ConnectionString";

        public const string KEY_TEST_SQLSERVER_CONNECTION   = "SqlServerConnectionString";
        public const string KEY_TEST_MYSQL_CONNECTION       = "MySqlServerConnectionString";

        public const string KEY_CONNECTIONSTRING = "ConnectionString";


        /// <summary>
        /// Sets the value of the config file's databaseType key at runtime.
        /// </summary>
        /// <param name="databaseType">Type of the database.</param>
        public static void SetConfigFileDatabaseType(string databaseType)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings["DatabaseType"].Value = databaseType;

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// Sets the config file connection string to 
        /// the specified key (use the "KEY_TEST_xxx" constants
        /// for the key names)..
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        public static void SetConfigFileConnectionStringToKey(string connectionString)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings[KEY_CONNECTION_STRING].Value = connectionString;

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }



        /// <summary>
        /// Gets the key value for the specified key.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        /// <returns></returns>
        public static string GetKeyValue(string keyName)
        {
            return ConfigurationManager.AppSettings[keyName];
        }
    }



}
